/**
 * Created by meiguizhinian on 2018/7/23
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIClipBox = ivy.UIBase.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();

		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	initWith: function (formName, blockName, sourceControlData) {
		this.init();
		this.setUIElementName(formName, blockName);
		let uim = ivy.UIManager.getInstance();
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;
		this.setLocalZOrder(layerID);
		this._width = w;
		this._height = h;
		this.setAnchorPoint(cc.p(0.5,0.5));
		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x+ w/2.0, y+h/2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		let anchorType = ivy.UIBase.UIAnchorType.eAnchorHV;
		this.setAnchorType(anchorType);

		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_CliperBox_EnterAction_action]));
		this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_CliperBox_OutAction_action]));

		this.alphaThreshold_ = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_CliperBox_AlphaThreshold_float]);
		let colorValue = uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_CliperBox_BgColor_color]);
		this._isInverted = uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_CliperBox_isInverted_list]) === 1;
		this.clipNode_ = new cc.ClippingNode();
		this.clipNode_.setAlphaThreshold(this.alphaThreshold_);
		this.clipNode_.setInverted(this._isInverted);

		if (colorValue !== 0) {
			this.bgColor_ = ivy.Tools.getColor4BFromInt32(colorValue);
			this.bgLayer_ = new cc.LayerColor(this.bgColor_, this._width, this._height);
			this.clipNode_.addChild(this.bgLayer_);
		}
		let stencilNodeNames = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_CliperBox_stencilNodeNames_string]);
		this.stencilNode_ = new cc.Sprite(stencilNodeNames);
		this.addChild(this.clipNode_);
		if (this.stencilNode_) {
			this.stencilNode_.setPosition(w / 2, h / 2);
			this.clipNode_.setStencil(this.stencilNode_);
		}
		//////////////////////////////////////////////////////////////////////////
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1) {
			this.setVisible(false);
		}
	},

	onChildCreateComplete : function() {
		let children = this.getChildren();
		for (let uibase of children) {
			if (uibase instanceof ivy.UIBase) {
				ivy.Tools.pushFunctionToCocosThread(function() {
					uibase.retain();
					uibase.removeFromParent();
					this.clipNode_.addChild(uibase);
					uibase.runEnterAction();
					uibase.release();
				}.bind(this));
			}
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	alphaThreshold_ : 0.0,
	_isInverted : false,
	bgColor_ : null,//cocos2d::Color4B
	clipNode_ : null,//cocos2d::ClippingNode*
	bgLayer_ : null,//cocos2d::LayerColor*
	stencilNode_ : null,//cocos2d::Sprite*
	_width : 0,
	_height : 0,
	stencilNodeNameVec_ : null,//std::vector<std::string>
});

ivy.UIClipBox.create = function (formName, blockName, sourceControlData) {
	return new ivy.UIClipBox(formName, blockName, sourceControlData);
};
